/**
 * 文件名：StaffServiceImpl
 * 版权：Copyright 2017-2022 CMCC All Rights Reserved.
 * 描述：
 */

package com.hand.project.service.impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.hand.project.dao.StaffDao;
import com.hand.project.domain.Staff;
import com.hand.project.listener.ExcelListener;
import com.hand.project.service.StaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;


/**
 *
 *
 *
 * @author shuai.wang02@hand-china.com
 * @version 1.0
 * @date 2018/11/12 15:42
 */
@Service
public class StaffServiceImpl  implements StaffService {

    private static Logger LOGGER = LoggerFactory.getLogger(StaffServiceImpl.class);
    @Autowired
    private StaffDao staffDao;

    @Override
    public Long saveStaff(Staff staff) {
        return staffDao.saveStaff(staff);
    }

    @Override
    public void excelwithReflectModel()  {
        Resource resource =new ClassPathResource("/loan1.xls");
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
        } catch (IOException e) {
            LOGGER.error("读取excel文件时异常",e);
        }
        try {
            // 解析每行结果在listener中处理
            AnalysisEventListener listener = new ExcelListener();
            ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
            excelReader.read(new Sheet(1, 1, Staff.class));
        } catch (Exception e) {
            LOGGER.error("解析excel流时异常",e);
        } finally {
            LOGGER.info("excel解析完成");
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.error("关闭excel流时异常",e);
            }
        }

    }
}
